** Master do file for:

// "Legislature Integration and Bipartisanship: A Natural Experiment in Iceland"
// Matt Lowe, UBC
// Donghee Jo, Uber 

clear all
set more off, permanently 
set maxvar 120000
set scheme plotplainblind
cap ssc install swapval
cap ssc install ivreghdfe
cap ssc install ranktest

global path_pch "C:/Users/Matt Lowe/Dropbox/Political Contact Hypothesis/Replication Files" // FIX FILEPATH TO REPLICATION FILES FOLDER

local prep 		"$path_pch/Code/prep"
local sims		"$path_pch/Code/simulations"
local analysis 	"$path_pch/Code/analysis"
local saia		"$path_pch/Code/saia"
local data 		"$path_pch/Data"
local output	"$path_pch/Output"

** preparation **
local switch_prep		= 0

** simulations **
local switch_sims		= 0

** analysis **
local switch_analysis	= 0

** saia replication and simulations **
local switch_saia		= 1




if `switch_prep' == 1 {
	
	** MP bio, manually scraped (manual changes all recorded as stata code)
	// input:	(1) $path_pch/Data/raw/Seating/althingi journals/digitised/seating_journal_`i'.csv 
						// seating arrangement for sessions 115-120, digitized from pdf journals
	//			(2) $path_pch/Data/raw/Seating/initialassign/seating_scraped_`i'.csv 
						// seating arrangement for sessions 121-149, web scraped from speech log
	//			(3) $path_pch/Data/raw/MP/PoliticianData.xlsx 
						// manually collected from Web:
						// e.g.,http://www.althingi.is/altext/cv/is/?nfaerslunr=141						
	//			(4) $path_pch/Data/raw/Co-Sponsorship/cosponsorship.csv 
						// scraped cosponsorship data
	//			Note. 	(1),(2),(4): essential to get list of politicians that we need to collect info.
	//					(3): not essential, just used to get the first suggestion of the gender of some MPs.
	cap erase "$path_pch/Data/intermediate/MP/MP_Bio.dta"
	run "`prep'/Iceland_MP_bio_prep.do"	
	

	** MP experience, separately collected
	// input:	(1) $path_pch/Data/raw/MP/mp_exper.csv 
						// scraped from althingi website, from career history section 
						// e.g. 'https://www.althingi.is/altext/cv/is/thingseta/?nfaerslunr=1234'
	cap erase "$path_pch/Data/intermediate/MP/MP_session_careers.dta"
	run "`prep'/Iceland_MP_exper_clean.do"	


	** MP data, seating data prep (connect the two)
	// input:	(1) $path_pch/Data/raw/Seating/althingi journals/digitised/seating_journal_`i'.csv 
						// seating arrangement for sessions 115-120, digitized from pdf journals
	//			(2) $path_pch/Data/raw/Seating/initialassign/seating_scraped_`i'.csv 
						// seating arrangement for sessions 121-149, web scraped from speech log.
	//			(3) $path_pch/Data/intermediate/MP/MP_Bio.dta 
						// MP bio info, collected above.
	// 			(4) $path_pch/Data/raw/MP/MP_dob.csv 
						// MP date of birth, scraped from althingi website, bio info section 
						// e.g. 'https://www.althingi.is/altext/cv/is/?nfaerslunr=1234'
	//			(5) $path_pch/Data/intermediate/MP/MP_session_careers.dta 
						// MP career data, collected above.
	//			(6) $path_pch/Data/raw/MP/mp_salaries_expenses.csv 
						// Expenses data since 2007 from https://www.althingi.is/altext/cv/is/laun_og_greidslur/
	//			(7) $path_pch/Data/raw/Seating/front_back_seat.csv 
						// definition of front and back neighbor, created by eyeballing by Matt.
	//			(8) $path_pch/Data/raw/Parties/parlgov.xlsx 
						// ParlGov left-right scores downloaded from https://parlgov.org/data-info/
	cap erase "$path_pch/Data/intermediate/Seating/seating_MP.dta" // upper and lower (i.e., left and right) only
	cap erase "$path_pch/Data/intermediate/Seating/seating_MP_frontback.dta" // including front and back neighbors too
	run "`prep'/Iceland_MPs_prep.do"
	
	
	** Prep voting data
	// input: 	(1) $path_pch/Data/raw/Voting/vote_info.csv
						// (has information about each vote: vote level data)
						// scraped from: http://www.althingi.is/thingstorf/thingmalin/atkvaedagreidsla/?nnafnak=51983
	//			(2) $path_pch/Data/raw/Voting/vote_ind.csv
						// (has individual roll call voting record: MP-vote level data)
						// scraped from: http://www.althingi.is/thingstorf/thingmalin/atkvaedagreidsla/?nnafnak=51983
	//			(3) $path_pch/Data/intermediate/Seating/seating_MP_frontback.dta
	//			(4) $path_pch/Data/intermediate/MP/MP_bio
	//			(5) $path_pch/Data/raw/MP/PartyLeaders1991to2018.xslx
						// party leader data
	//			(6) $path_pch/Data/raw/Voting/voting_procedure_with_chairman_vote_and_senior_MP_saia.dta
	cap erase "$path_pch/Data/analysis/Voting/voting_analysis.dta"
	run "`prep'/Iceland_voting_prep.do"
	
	
	** Clean bill categories
	// input:	(1) $path_pch/Data/raw/Voting/bill_type/bill_categories.xlsx
	cap erase "$path_pch/Data/intermediate/Voting/bill_type/bill_cat_clean.dta"
	run "`prep'/Iceland_billtype_prep.do"
	
	
	** MP-session-level data prep
	// input:	(1) $path_pch/Data/analysis/Voting/voting_analysis.dta
	//			(2) $path_pch/Data/intermediate/MP/MP_bio.dta
	//			(3) $path_pch/Data/raw/Co-Sponsorship/cosponsorship.csv
	//			(4) $path_pch/Data/raw/Co-Sponsorship/resolutions.csv
	//			(5) $path_pch/Data/raw/Co-Sponsorship/reports.csv
	//			(6) $path_pch/Data/intermediate/Seating/seating_MP.dta
	cap erase "$path_pch/Data/analysis/Voting/MP_session_analysis.dta"
	run "`prep'/Iceland_MPsession_prep.do"	
	
		
	** Prep for voting (dyadic)
	// input:	(1) "$path_pch/Data/raw/Voting/vote_info_20210626.csv"
	//			(2) "$path_pch/Data/intermediate/Voting/bill_type/bill_cat_clean.dta"
	//			(3) "$path_pch/Data/analysis/Voting/voting_analysis.dta"
	//			(4) "$path_pch/Data/intermediate/Seating/seating_MP_frontback.dta"
	//			(5) "$path_pch/Data/intermediate/Seating/seating_MP.dta"
	cap erase "$path_pch/Data/intermediate/Voting/voting_MP_dyad.dta"
	cap erase "$path_pch/Data/analysis/Voting/dyads_voting.dta"
	run "`prep'/Iceland_voting_prep_dyad.do"
		
		
	** Cosponsor data prep (dyadic)
	// input: 	(1) $path_pch/Data/raw/Co-Sponsorship/cosponsorship.csv 
	//			(2) $path_pch/Data/raw/Co-Sponsorship/resolutions.csv
	//			(3) $path_pch/Data/raw/Co-Sponsorship/reports.csv	
	//			(4) $path_pch/Data/intermediate/Seating/seating_MP.dta	
	//			(5) "$path_pch/Data/intermediate/Seating/seating_MP_frontback.dta"	
	foreach x in cospon rep res {
		foreach y in "" "_1st" "_2nd" "_m1" "_m2" "_m3" "_m4" "_m5" "_m6" "_m7" "_m8" "_m9" "_m10" "_m11" "_m12" {
			cap erase "$path_pch/Data/intermediate/Co-Sponsorship/`x'_billMPlevel`y'.dta"
		} 
	}
	cap erase "$path_pch/Data/intermediate/Co-Sponsorship/busy_months.dta"
	cap erase "$path_pch/Data/analysis/Co-Sponsorship/dyads_with_cospon.dta"
	run "`prep'/Iceland_cosponsor_prep_dyad.do"

}

if `switch_sims' == 1 {
	
	** Run model simulations
	// input:	(1) "$path_pch/Data/intermediate/Seating/seating_MP_frontback.dta"
	//			(2) "$path_pch/Data/analysis/Voting/voting_analysis.dta"
	foreach x in 1 2 3 5 7 10 15 20 30 {
		foreach y in 1to10 11to20 21to30 31to40 41to50 {
			cap erase "$path_pch/Data/intermediate/Simulated/sim_MP_dyad_RI`y'_`x'perc.dta"
			cap erase "$path_pch/Data/intermediate/Simulated/sim_MP_sess_RI`y'_`x'perc.dta"	
			cap erase "$path_pch/Data/intermediate/Simulated/votes_simulated_RI`y'_`x'perc.dta"			
		}
	}
	do "`sims'/0_pair_general_sim_master.do" // this calls three more do-files
		// ~45 mins per parameter/RI run combination, for 5*9=45 combinations
		// so ~33.75 hours to run this switch 
		// note: these simulations do not replicate exactly the simulated data used in the paper (due to seed issues)
		// so to replicate exact paper results, don't run this switch
}

if `switch_analysis' == 1 {

	** Create all tables and figures
	// input:	(1) $path_pch/Data/analysis/Voting/dyads_voting.dta
	//			(2) $path_pch/Data/analysis/Co-Sponsorship/dyads_with_cospon.dta
	//			(3) $path_pch/Data/analysis/Voting/MP_session_analysis.dta
	//			(4) $path_pch/Data/raw/Co-Sponsorship/bills passed/bills_passed.dta 
						// scraped from Althingi website
	//			(5) $path_pch/Data/intermediate/Voting/bill_type/bill_cat_clean.dta
	//			(6) simulated data in $path_pch/Data/intermediate/Simulated
	//			(7) $path_pch/Data/raw/Voting/vote_info_20210626.csv
	//			(8) $path_pch/Data/analysis/Voting/voting_analysis.dta
	run "`analysis'/pch_tables_jop.do"

}

if `switch_saia'==1 {
	
	** Create vote simulation files for Saia replication and simulations
	// input:	(1) $path_pch/Data/intermediate/Seating/seating_MP_frontback.dta
	//			(2) $path_pch/Data/raw/Voting/vote_info.csv
	//			(3) $path_pch/Data/raw/Voting/voting_procedure_with_chairman_vote_and_senior_MP_saia
	cap erase "$path_pch/Data/intermediate/Saia/saia_simul_vote.dta"
	foreach x in U L F B {
		cap erase "$path_pch/Data/intermediate/Saia/saia_simul_vote_`x'.dta"
	}
	run "`saia'/saia_simulation.do"	
	

	** Run Saia regressions with re-randomized seating (took ~30 hours)
	// input:	(1) $path_pch/Data/intermediate/Saia/saia_simul_vote.dta + _U/L/F/B
	//			(2) $path_pch/Data/intermediate/Seating/seating_MP_frontback.dta
	cap erase "$path_pch/Data/intermediate/Saia/saia_simul_results.dta"
	run "`saia'/saia_reruns.do"
		
	
	** Create Figure C1 using output of regressions using re-randomized seating
	// input: 	(1) $path_pch/Data/intermediate/Saia/saia_simul_results
	cap erase "`output'/Figures/saia_simul_figC1.png"
	run "`saia'/saia_figure.do"
	
	
	** Create Table C1 (took ~2 hours)
	// input:	(1) $path_pch/Data/intermediate/Saia/saia_simul_vote.dta + _U/L/F/B
	//			(2) $path_pch/Data/intermediate/Seating/seating_MP_frontback.dta
	cap erase "$path_pch/Data/analysis/saia_table_analysisfile.dta"
	cap erase "$path_pch/Output/Tables/saiatableC1.tex"
	cap erase "$path_pch/Output/Tables/saiatableC1A.tex"	
	do "`saia'/saia_table.do"

}

beep
